/*
 * File:   form_sharpen_filter.cpp
 * Author: dima
 *
 * Created on 7 февраля 2015 г., 23:44
 */
#include <QMessageBox>
#include "ui_form_sharpen_filter.h"
#include "form_sharpen_filter.h"
#include "ipd_exception.h"
#include "ipd_sharpening_spatial_filter.h"

form_sharpen_filter::form_sharpen_filter(ipd::ipd_img& image) : original_image(image), widget(new Ui::form_sharpen_filter())
{
    widget->setupUi(this);
    filter = NULL;
    convertible_image = new ipd::ipd_img();
}

form_sharpen_filter::~form_sharpen_filter()
{
    delete convertible_image;
    delete_filter();
    delete widget;
}

void form_sharpen_filter::convert_image()
{
    widget->convert->setEnabled(false);
    delete_filter();
    ipd::tp_sharpening_spatial_filter tp_filter = ipd::component_mask_laplacian_1;
    try
    {
        if (widget->laplacian_mask_2->isChecked())
            tp_filter = ipd::component_mask_laplacian_2;
        filter = new ipd::ipd_sharpening_spatial_filter(original_image);
        filter->init(tp_filter);
        filter->convert_image(*convertible_image);
    }
    catch (ipd::ipd_exception &except)
    {
        QMessageBox::critical(0, QString::fromUtf8(except.in_file().c_str()), QString::fromUtf8(except.what().c_str()), 0, 0);
    }
    emit send_image(*convertible_image);
    widget->convert->setEnabled(true);
}

void form_sharpen_filter::delete_filter()// delete filter
{
    if (filter != NULL)
        delete filter;
    filter = NULL;

}